#include "CdiMovementCamera.h"

CdiMovementCamera::CdiMovementCamera(LPDIRECT3DDEVICE9 pDevice)
: m_pDevice( pDevice )
{
  m_ready = false;
  m_centerX = m_centerY = -1;
  SetScale( 10 );
}

CdiMovementCamera::CdiMovementCamera( LPDIRECT3DDEVICE9 pDevice, D3DXVECTOR3& position, D3DXVECTOR3& lookAt, D3DXVECTOR3& up )
: CdiCamera( position, lookAt, up ) 
{
  m_centerX = m_centerY = -1;
  m_ready = false;
  AssertValid();
}

CdiMovementCamera::~CdiMovementCamera()
{
  m_pDevice = NULL;
}

void CdiMovementCamera::HandleInput(UINT Msg, WPARAM wParam, LPARAM lParam)
{
  if( ! IsReady() ) 
    return;

  switch( Msg ) 
  {
  case WM_MOUSEMOVE:
    {
      if( m_centerX == -1 || 
          m_centerY == -1 
        )
        return;
      int x = LOWORD( lParam );
      int y = HIWORD( lParam );
      MoveCameraAngle( (float)x, (float)y, 0.0f);

    } break;

  case WM_KEYDOWN:
    {
      switch( wParam )
      {
      case VK_UP:
        {
          MoveCameraAndView( 0.0f, 0.0f, -1.10f );
        } break;
      case VK_DOWN:
        {
          MoveCameraAndView( 0.0f, 0.0f, 1.10f );
        } break;
      case VK_LEFT:
        {
          MoveCameraAndView( 1.10f, 0.0f, 0.0f );
        } break;
      case VK_RIGHT:
        {
          MoveCameraAndView( -1.10f, 0.0f, 0.0f );
        } break;
      }
    } break;

  }
}

// Sets the camera to start handling the input
void CdiMovementCamera::SetCameraReady(bool CameraReady)
{
  m_ready = CameraReady;
}

// Set's the mouses current middle position
void CdiMovementCamera::SetWindowMiddle(int x, int y)
{
  m_centerX = x;
  m_centerY = y;
}

// Move the Camera and the viewing
void CdiMovementCamera::MoveCameraAndView(float x, float y, float z )
{
  m_lookAt.x -= (x/m_scale);
  m_lookAt.z -= (z/m_scale);
  
  m_position.x -= (x/m_scale);
  //m_position.y -= (y/m_scale);
  m_position.z -= (z/m_scale);
}

// Moves the camera Angle
void CdiMovementCamera::MoveCameraAngle(float x, float y, float z)
{
  /*x = (x * m_scale) * 3.14156f;
  y = (y * m_scale) * 3.14156f;
  D3DXMATRIXA16 lookAtMatrix;
  D3DXMatrixIsIdentity( &lookAtMatrix );
  D3DXMatrixRotationX( &lookAtMatrix, x );
  D3DXMatrixRotationY( &lookAtMatrix, y );
  m_lookAt.x += lookAtMatrix._31;
  m_lookAt.y += lookAtMatrix._32;
  m_lookAt.z += lookAtMatrix._33;*/
}

